package com.qfedu.aunt.commons.service.impl;

import com.qfedu.aunt.commons.aop.BehaviorLog;
import com.qfedu.aunt.commons.auth.security.TokenManager;
import com.qfedu.aunt.commons.pojo.Behavior;
import com.qfedu.aunt.commons.mapper.BehaviorMapper;
import com.qfedu.aunt.commons.service.BehaviorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaobobo
 * @since 2022-11-15
 */
@Service
public class BehaviorServiceImpl extends ServiceImpl<BehaviorMapper, Behavior> implements BehaviorService {

    @Autowired
    private TokenManager tokenManager;

    @Autowired
    private BehaviorMapper behaviorMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String add(ProceedingJoinPoint proceedingJoinPoint, BehaviorLog behaviorLog) {
        //首先我们要要获取谁?
        //获取我们的请求对象
        String username=getUserNameByToken();
        //在什么时候 ?
        Date currentTime = new Date();
        //调用了那个模块?
        String modelName = behaviorLog.modelName();
        //调用了那个方法
        String methodName = behaviorLog.methodName();

        StringBuffer stringBuffer=new StringBuffer();

        Behavior behavior = new Behavior();
        behavior.setUsername(username);
        behavior.setMethodName(methodName);
        behavior.setModelName(modelName);
        behavior.setStartTime(currentTime);
        behavior.setStatus(1);


        if(behaviorLog.paramExists()){
            stringBuffer.append("[");
            //传递了什么参数
            Object[] args = proceedingJoinPoint.getArgs();
            //要转换成String类型
            for (int i = 0; i <args.length ; i++) {
                stringBuffer.append(args[i]+",");
            }
            //执行到这里多个个逗号

            String substring = stringBuffer.toString().substring(0, stringBuffer.toString().lastIndexOf(","));
            substring+="]";

            behavior.setParam(substring);
        }




        //接下来向数据库做记录
        behaviorMapper.insert(behavior);

        return behavior.getBehaviorId();
    }
    @Override
    public void updateBehavior(String behaviorId, int status) {
        //首先要查询这个数据
        Behavior behavior = behaviorMapper.selectById(behaviorId);
        if(null!=behavior){
           //接下来才是更新
            behavior.setStatus(status);
            //接下来实施更新
            behaviorMapper.updateById(behavior);
        }
    }

    /**
     * 获取咋们的token
     */
    public String getUserNameByToken() {
        HttpServletRequest req = ((ServletRequestAttributes) (RequestContextHolder.getRequestAttributes())).getRequest();
        //接下来：获取token
        String token = req.getHeader("token");
        //解析我们的用户名
        String username = tokenManager.getUserNameFromToken(token);
        return username;
    }
}
